#!/usr/bin/env python3
import threading
from multiprocessing import Queue #引入多线程队列
from subprocess import Popen,PIPE #引入子进程和管道,子进程可以执行命令

class C_search(threading.Thread):
    def __init__(self,queue):
        threading.Thread.__init__(self)#初始化self
        self.queue = queue#初始化队列
    def run(self):
        #获取ip地址
        while not self.queue.empty():
            ip = self.queue.get()#接收队列
            try:
                check_ping = Popen("ping {} \n".format(ip),stdin = PIPE,stdout = PIPE,shell = True)
                data = check_ping.stdout.read()
                if "TTL" in str(data):
                    print(ip+" is up .\n")
                else:
                    print(f"{ip} 主机不在线")
            except:
                pass
def start(count):
    queue = Queue()
    for i in range(1,255):
        queue.put('192.168.1.'+str(i))#向队列传入ip
        #print('正在扫描192.168.1.'+str(i))
    #开启多线程
    Threads = []
    Thread_count = int(count)
    for i in range(Thread_count):
        Threads.append(C_search(queue))
    for t in Threads:
        t.start()
    for t in Threads:
        t.join()

if __name__ == "__main__":
    start("16")#调整线程数
